/*
** my_separate_redirection.c for 42sh in /home/lamais_a//minishell2-2017-lamais_a
** 
** Made by adrien lamaison
** Login   <lamais_a@epitech.net>
** 
** Started on  Wed Jun 12 17:11:15 2013 adrien lamaison
** Last update Sat Jun 15 13:52:40 2013 adrien lamaison
*/

#include "minishell.h"
#include <string.h>

int		my_char_simple(char c)
{
  if (c == '>' || c == '<')
    return (1);
  return (0);
}

int		my_char_double(char *str, int count)
{
  if (count < my_strlen(str) - 2)
    {
      if ((my_char_simple(str[count]) == 1) && (my_char_simple(str[count + 1])) == 1)
	return (1);
    }
  return (0);
}

int		count_nb_word(char *str)
{
  int		count;
  int		nb_word;

  count = 0;
  nb_word = 0;
  while (str[count])
    {
      if (my_char_double(str, count) ==  1)
	{
	  count += 2;
	  if (my_char_simple(str[count]) != 0 || str[count] == 0 || count - 2 == 0)
	    return (my_error());
	  nb_word++;
	}
      else if (my_char_simple(str[count]) == 1)
	{
	  count++;
	  if (str[count] == 0 || count - 1 == 0)
	    return (my_error());
	  nb_word++;
	}
      else
	count++;
    }
  return (nb_word + 1);
}

int		count_nb_letter(char *str, int count)
{
  int		count2;
 
  count2 = 0;
  while (my_char_simple(str[count + count2]) != 1 
	 && my_char_double(str, count + count2))
      count2++;
  return (count2);
}

t_redirection	*cpy_str_in_tab(t_redirection	*my_redirection, 
				char *str, int nb_word)
{
  int		word;
  int		count;
  int		nb_letter;

  count = 0;
  word = 0;
  while (str[count] != 0 && word < nb_word)
    {
      nb_letter = count_nb_letter(str, count);
      my_redirection->tab[word] = malloc((nb_letter + 1) * sizeof(char));
      if (my_redirection->tab[word] == NULL)
	return (NULL);
      my_redirection->tab[word][nb_letter] = '\0';
      my_redirection->tab[word] = cpy_str(my_redirection->tab[word], str, &count);
      if (my_char_double(str, count) == 1)
	my_redirection = cpy_double(my_redirection, str, &count, word);
      else if (my_char_simple(str[count]) == 1)
	my_redirection = cpy_simple(my_redirection, str, &count, word);
      if (my_redirection == NULL)
	return (NULL);
      word++;
     }
  return (my_redirection);
}

t_redirection	*my_separate_redirection(char *str)
{
  t_redirection	*my_redirection;
  int		nb_word;
  
  nb_word = count_nb_word(str);
  if (nb_word == -1)
    return (NULL);
  my_redirection = malloc(sizeof(t_redirection));
  if (my_redirection == NULL)
    return (NULL);
  my_redirection->tab = malloc((nb_word + 1) * sizeof(char));
  my_redirection->redirection = malloc(nb_word * sizeof(char));
  if (my_redirection->tab == NULL || my_redirection->redirection == NULL)
    return (NULL);
  my_redirection->redirection[nb_word - 1] = NULL;
  my_redirection = cpy_str_in_tab(my_redirection, str, nb_word);
  free(str);
  return (my_redirection);
}

int		main()
{
  t_redirection	*my_redirection;
  char		**tab;
  char		*buffer = malloc(50 * sizeof(char));
  int		ret;
  int		count;

  buffer = memset(buffer, 0, 50);
  ret = read(0, buffer, 49);
  buffer[ret - 1] = 0; 
  my_redirection = my_separate_redirection(buffer);
  if (my_redirection == NULL)
    return (-1);
  while (my_redirection->tab[count] != NULL)
    {
      my_putstr(my_redirection->tab[count]);
      my_putchar('\n');
      count++;
    }
  return (0);
}
